# -*- coding: utf-8 -*-
from google.appengine.ext import webapp,db
from google.appengine.ext.webapp import util,template
from google.appengine.api import memcache

import os,datetime

ENTRY_EACH_PAGE=10
MEMCACHE_TIME=360000
LOGIN_MEMCACHE_TIME=3600

def GetTime():
    return datetime.datetime.now()+datetime.timedelta(hours=8)

def CheckState(self):
    return memcache.get(self.request.remote_addr)

def GetFace(name):
    return db.GqlQuery('SELECT * FROM admins WHERE name=:1',name)[0].image

class admins(db.Model):
    name=db.StringProperty()
    password=db.StringProperty()
    image=db.StringProperty()
    
class Entry(db.Model):
    uid=db.IntegerProperty()
    date=db.DateTimeProperty()
    content=db.StringProperty(multiline=True)
    author=db.StringProperty()
    side=db.IntegerProperty()
    image=db.StringProperty()

def MakeIndex(handler,pid):
    total_page=Entry.all().count()/ENTRY_EACH_PAGE
    if Entry.all().count() > total_page * ENTRY_EACH_PAGE:
        total_page+=1
    html='''<p class="index_index" onmouseover="this.className='index_index_hover'" onmouseout="this.className='index_index'"><a href='/%s'>%s</a></p> '''
    output=''
    if total_page<=3:
        for p in range(1,total_page+1):
            output+=html % (p,p)
    elif total_page>3 and total_page<6:
        if pid==1:
            for p in range(1,4):
                output+=html % (p,p)
            output+='... '
            output+=html % (total_page,total_page)
        elif pid==6:
            output+=html % (1,1)
            for p in range(4,7):
                output+=html % (p,p)
        else:
            for p in range(1,total_page):
                output+=html % (p,p)
    elif total_page >6:
        if pid < 3:
            for p in range(1,4):
                output+=html % (p,p)
            output+='... '
            output+=html % (total_page,total_page)
        elif pid > total_page-2:
            output+=html % (1,1)
            output+='... '
            for p in range(total_page-2,total_page+1):
                output+=html % (p,p)
        else:
            output+=html % (1,1)
            output+='... '
            for p in range(pid-1,pid+2):
                output+=html % (p,p)
            output+='... '
            output+=html % (total_page,total_page)
    else:
        return handler.Error(u'目录姬傲娇中')

    return output

class RequestHandler(webapp.RequestHandler):
    '''My RequestHandler to deal with somethings such as render or else'''
    def Render(handler,_f,_t):
        return handler.response.out.write(template.render(os.path.join(os.path.dirname(__file__),'templates/%s' % _f) \
                                                ,_t))
    def Error(handler,d=u'',e=u'少女傲娇中'):
        template_values={
            'error':e,
            'detail':d,
            }
        return handler.response.out.write(template.render(os.path.join(os.path.dirname(__file__),'templates/error.html') \
                                                , template_values))
